package com.learn.russianRoulette.service;

import com.learn.russianRoulette.entity.role;

import java.util.Scanner;
import java.util.Random;
public class gameService {
    public void gameStart(role role1, role role2) {
        System.out.println("我的天，你终于醒来了");
        Scanner scanner = new Scanner(System.in);
        role1.setRoleName("你");
        role1.setHealth(1);
        pauseForOneSecond(1);
        System.out.println("你不记得发生了什么吗？你被人栽赃入狱，即将执行死刑!");
        pauseForOneSecond(2.5);
        System.out.println("但是不要怕，典狱长开恩惠，给你一次机会，你将和三名死刑犯争夺一个生的机会，每次你将面对一名凶险至极的死刑犯");
        pauseForOneSecond(2.5);
        System.out.println("你们的面前会有一把枪，枪里有空弹和实弹，弹药随机装填，请牢记规则：\n");
        pauseForOneSecond(2.5);
        System.out.println("###\n每回合都要选择 向对方开枪 或 向自己开枪\n" +
                "-如果选择向自己开枪，则下回合还是你的回合\n-如果选择向对方开枪，下回合变成对方回合\n" +
                "先打死对方的人获胜\n###");
        pauseForOneSecond(7.5);
        System.out.println("\n对方可是真正的死刑犯，不要犹豫扣下扳机！(回车继续)");
        scanner.nextLine();
    }
    public int whoFirst() {
        System.out.println("通过抛硬币决定先手，你选择硬币正面还是反面？（0正面/1反面）");
        Scanner scanner = new Scanner(System.in);
        int index = scanner.nextInt();
        switch (index){
            case 0:
                System.out.println("你选择正面");
                break;
            case 1:
                System.out.println("你选择反面");
                break;
        }
        pauseForOneSecond(1.0);
        Random random = new Random();
        int flag = random.nextInt(2);
        switch (flag){
            case 0:
                System.out.println("硬币是正面\n");
                break;
            case 1:
                System.out.println("硬币是反面\n");
                break;
        }
        pauseForOneSecond(1.0);
        if(index == flag){
            System.out.println("选择正确，你先手");
            return 0;
        }
        else{
            System.out.println("选择错误，你后手");
            return 1;
        }
    }
    public int shotRole1(role role1, role role2, int[] bullets, int flag) {
        int countine = 0;
        System.out.println("枪里还有" + (bullets.length-flag) + "发子弹，请选择开枪方向(0向对方开枪 1向自己开枪)");
        Scanner scanner = new Scanner(System.in);
        int index = scanner.nextInt();
        switch (index) {
            case 0:
                pauseForOneSecond(1.0);
                System.out.println("你选择了向 对方 开枪");
                waitShot(0.5);
                role1.shot(role1, role2, bullets, flag);
                countine = 1;
                break;
            case 1:
                pauseForOneSecond(1.0);
                System.out.println("你选择了向 自己 开枪");
                waitShot(0.5);
                role1.shot(role1, role1, bullets, flag);
                break;
        }
        return countine;
    }
    public int shotBoss(role roleBoss, role role1, int[] bullets, int flag) {
        int countine = 1;
        int index;
        System.out.println("枪里还有" + (bullets.length-flag) + "发子弹，现在你的对手来做选择");
        pauseForOneSecond(1.0);
        if(flag==bullets.length-1){
            index = 0;
        }
        else{
            Random random = new Random();
            index = random.nextInt(2);
        }
        switch (index) {
            case 0:
                pauseForOneSecond(1.0);
                System.out.println("你的对手选择了向 你 开枪");
                waitShot(0.5);
                roleBoss.shot(roleBoss, role1, bullets, flag);
                countine = 0;
                break;
            case 1:
                pauseForOneSecond(1.0);
                System.out.println("你的对手选择了向 自己 开枪");
                waitShot(0.5);
                roleBoss.shot(roleBoss, roleBoss, bullets, flag);
                break;
        }
        return countine;
    }
    public static void pauseForOneSecond(double second) {
        // 将秒转换为毫秒，因为Thread.sleep方法需要毫秒参数
        second = second * 1000;
        try {
            // 使当前线程睡眠指定的毫秒数
            Thread.sleep((int)second);
        } catch (InterruptedException e) {
            // 当线程被中断时，捕获InterruptedException并打印堆栈跟踪
            e.printStackTrace();
        }
    }
    public static void waitShot(double s) {
        pauseForOneSecond(s);
        System.out.println("3");
        pauseForOneSecond(s);
        System.out.println("2");
        pauseForOneSecond(s);
        System.out.println("1");
        pauseForOneSecond(s+1.0);
    }
}
